package com.atguigu.msmService.service.impl;

import com.atguigu.msmService.constant.SmsConstant;
import com.atguigu.msmService.service.MsmService;
import com.atguigu.msmService.utils.Client;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

@Service
@Transactional
public class MsmServiceImpl implements MsmService {



    //发送短信验证码
    @Override
    public Boolean sendMsm(Map<String, Object> map, String phone) {



        if (StringUtils.isEmpty(phone)) {
            return false;
        }
        Client client = new Client();
        client.setAppId(SmsConstant.APP_ID);     //开发者ID，在【设置】-【开发设置】中获取
        client.setSecretKey(SmsConstant.SECRET_KEY);    //开发者密钥，在【设置】-【开发设置】中获取
        client.setVersion("1.0");

        Client.Request request = new Client.Request();
        request.setBizContent("{\"mobile\":[\""+phone+"\"],\"type\":0,\"template_id\":\""+SmsConstant.TEMpLATE_ID+"\",\"sign\":\""+SmsConstant.SING+"\",\"send_time\":\"\",\"params\":{\"code\":"+map.get("code")+"}}");  // 这里是json字符串，send_time 为空时可以为null, params 为空时可以为null,短信签名填写审核后的签名本身，不需要填写签名id
        request.setMethod("sms.message.send");
        System.out.println( client.execute(request) );
        return true;
    }
}
